const mongoose = require('mongoose')
let Schema = mongoose.Schema

let FoodSchema = new mongoose.Schema({
    // food name
    foodName: {
        type: String,
        required: 'Please fill in a username',
    },

    // food unit price
    foodPrice: {
        type: String,
        required: true
    },

    // food picture
    foodPic: {
        type: String,
        default: 'http://img0.imgtn.bdimg.com/it/u=3918982731,1256537227&fm=26&gp=0.jpg'
    },

    // food type. (one to one)
    foodType: {
        type: Schema.Types.ObjectId,
        ref: 'FoodType'
    },

    // belone to merchant(one to  one)
    merchant: {
        type: Schema.Types.ObjectId,
        ref: 'Merchant'
    },

    meta: {         //date note
        createAt: {
            type: Date,
            default: Date.now()
        },
        updateAt: {
            type: Date,
            default: Date.now()
        }
    }
})

FoodSchema.pre('save', function (next) {
    if (this.isNew) {
        this.meta.createAt = this.meta.updateAt = Date.now()
    } else {
        this.meta.updateAt = Date.now()
    }
    next()
})

FoodSchema.statics = {
    findById: function (id, cb) {
        return this
            .findOne({ _id: id })
            .exec(cb)
    },
}

module.exports = FoodSchema